package com.zhaoc.service.impl;

import com.zhaoc.dao.PromotionRepository;
import com.zhaoc.domain.PageBean;
import com.zhaoc.domain.take_delivery.Promotion;
import com.zhaoc.service.IPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/12 0012
 * time:18:15
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class PromotionServiceImpl implements IPromotionService {

    @Autowired
    private PromotionRepository promotionRepository;
    //保存活动信息
    @Override
    public void save(Promotion model) {
     //调用dao层保存活动信息
     promotionRepository.save(model);
    }

    //查询所有活动
    @Override
    public Page<Promotion> findPageData(Pageable pageable) {
        return promotionRepository.findAll(pageable);
    }

    //根据前台传递过来的参数,查询分页的活动数据
    @Override
    public PageBean<Promotion> findPageData(int page, int rows) {
        Pageable pageable = new PageRequest(page - 1, rows);
        Page<Promotion> pageData = promotionRepository.findAll(pageable);

        //封装page对象
        PageBean<Promotion> pageBean = new PageBean<Promotion>();
        pageBean.setTotalCount(pageData.getTotalElements());
        pageBean.setPageData(pageData.getContent());
        return pageBean;
    }


    //根据ID查询
    @Override
    public Promotion findById(Integer id) {
        return promotionRepository.findOne(id);
    }
}
